/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package its.mx.dsw2.view;

import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.Bl.AssignScholarshipBLLocal;
import mx.its.dsw2.Bl.StudensBLLocal;
import mx.its.dsw2.Bl.scholarshipBlLocal;
import mx.itsc.ds2.Dao.AssignscholarshipDaoLocal;
import mx.itsc.ds2.model.Assignscholarship;

/**
 *
 * @author Martinez
 */
@ManagedBean
@SessionScoped
public class AssignScholarshipBean {

    //---------------------EJB's------------------------------------------------
    @EJB
    private scholarshipBlLocal scholarshipBl;
    @EJB
    private StudensBLLocal studensBL;
    @EJB
    private AssignScholarshipBLLocal assignScholarshipBL;
    @EJB
    private AssignscholarshipDaoLocal assignscholarshipDao;
    //--------------------------------------------------------------------------
    //---------------------VARIABLES--------------------------------------------
    private List<Assignscholarship> listAssignedScholarship;
    private Assignscholarship assignScholarship;
    private String action = "";
    private int schoarshipSelected;
    private int studentSelected;
    //--------------------------------------------------------------------------
    //--------------------------------GETTER'S AND SETTER'S---------------------

    public int getSchoarshipSelected() {
        return schoarshipSelected;
    }

    public void setSchoarshipSelected(int schoarshipSelected) {
        this.schoarshipSelected = schoarshipSelected;
    }

    public int getStudentSelected() {
        return studentSelected;
    }

    public void setStudentSelected(int studentSelected) {
        this.studentSelected = studentSelected;
    }

    public List<Assignscholarship> getListAssignedScholarship() {
        listAssignedScholarship = assignscholarshipDao.findAll();
        return listAssignedScholarship;
    }

    public void setListAssignedScholarship(List<Assignscholarship> listAssignedScholarship) {
        this.listAssignedScholarship = listAssignedScholarship;
    }

    public Assignscholarship getAssignScholarship() {
        return assignScholarship;
    }

    public void setAssignScholarship(Assignscholarship assignScholarship) {
        this.assignScholarship = assignScholarship;
    }

    public AssignScholarshipBean() {
        assignScholarship = new Assignscholarship();
    }
    //--------------------------------------------------------------------------
    //----------------------ACTIONS---------------------------------------------

    public void prepareNew(ActionEvent evt) {
        action = "New";
        assignScholarship = new Assignscholarship();

    }

    public void prepareEdit(ActionEvent evt) {
        action = "Edit";
        listAssignedScholarship = assignScholarshipBL.findAssignedScholarship(assignScholarship);
    }

    public void prepareRemove(ActionEvent evt) {
        action = "Delete";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        assignScholarship = listAssignedScholarship.get(index);
    }

    public String processSelection() {
        assignScholarship.setIdStudent(studensBL.findById(studentSelected));
        assignScholarship.setIdScholarship(scholarshipBl.findById(schoarshipSelected));
        return "AssignScholarshipConfirm";
    }

    public String processAction() {
        if (action.equalsIgnoreCase("New")) {
            assignScholarshipBL.register(assignScholarship);
        } else if (action.equalsIgnoreCase("Edit")) {
            assignScholarshipBL.modify(assignScholarship);
        }else if(action.equalsIgnoreCase("Delete")){
            assignScholarshipBL.delete(assignScholarship);
        }
        action = "";
        return "AssignScholarshipList";
    }
    //--------------------------------------------------------------------------
}
